// pages/totalorder/totalorder.js
import {request, requestWrap} from "../../request/index.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        dataList: [],
        isShow: false,
        times: [],
        index: 0
    },
    openid: '',
    student: '',
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.openid = wx.getStorageSync('openid')
        this.student = wx.getStorageSync('student')
        this.getData()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    async getData () {
        let res = requestWrap(await request({
            url:"api/wxapp/user/moenyList",
            method:'GET',
            data: {}
        }))
        if (res && res.code == 1) {
            if (res.data && res.data.length > 0) {
                this.setData({
                    dataList: res.data
                })
            } else {
                this.setData({
                    dataList: ''
                })
            }
        }
    },
    async onRefund (e) {
        let {index} = e.currentTarget.dataset
        let order_no = this.data.dataList[index].order_no
        let res = requestWrap(await request({
            url:"api/wxapp/user/refund",
            method:'GET',
            data: {
                order_no
            }
        }))
        this.getData()
        setTimeout(() => {
            wx.showToast({
                title: res.msg,
                icon: 'none'
            })
        }, 50)
    },
    onChange (e) {
        this.setData({
            index: e.detail.value
        })
    },
    hidePopup () {
      this.setData({
          isShow: false
      })
    },
    btnFlag: true,
    isTimes: false,
    studentId: '',
    orderId: '',
    async onPay (e) {
        // console.log(e)
        if (!this.btnFlag) return
        let {index} = e.currentTarget.dataset
        let orderData = this.data.dataList[index]
        this.orderId = orderData.id
        this.studentId = orderData.student_id
        if (orderData.is_times == 1) {
          this.isTimes = true
          let arr = []
          let num = parseInt(orderData.num)
          for (var i=1; i <= num; i++) {
            arr.push(i + '次')
          }
          // console.log(arr)
          this.setData({
            index: num - 1,
            isShow: true,
            times: arr
          })
        } else {
          this.isTimes = false
          this.signRequest()
        }
    },
    async payRequest () {
      if (!this.btnFlag) return
      let data = {}
      data.openid = this.openid
      data.student_id = this.studentId
      data.course_id = this.orderId
      if (this.isTimes) {
        data.num = parseInt(this.data.index) + 1
      }
      this.btnFlag = false
      let res = requestWrap(await request({
          url:"api/wxapp/student/courseOrder",
          method:'GET',
          data
      }))
      // console.log(res)
      // return
      if (res && res.code == 1) {
          wx.requestPayment({
          timeStamp: res.data.time.toString(),
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: res.data.signType,
          paySign: res.data.paySign,
          success: function(res) {
              // console.log(res)
              this.getData()
              setTimeout(() => {
                  wx.showToast({
                      title: '支付成功',
                      icon: 'none'
                  })
              }, 50)
          },
          fail: function(res) {
              // console.log(res)
              wx.showToast({
              title: res.msg,
              icon: 'none'
              })
          }
          })
      } else {
          wx.showToast({
          title: res.msg,
          icon: 'none'
          })
      }
    }
})